<?php
/**
 * Created by PhpStorm.
 * FileName: Appointment.class.php
 * User: Administrator
 * Date: 2018/4/16
 * Time: 10:02
 */

namespace Api\Controller;

use Admin\Model\ConfigModel;
use Admin\Model\OrderReviewModel;

class OrderReviewController extends ApiController {

    private $configModel;
    private $orderReviewModel;

    public function __construct() {
        parent::__construct();
        $this->configModel = new ConfigModel();
        $this->orderReviewModel = new OrderReviewModel();

    }

    public function getList() {
        $where = array();
        $order_id = I('order_id');
        if (!$order_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $userWhere['order_id'] = $order_id;
        $pageSize = 15;
        $curPage = I('p',1);
        $data = $this->orderReviewModel->getList($where,$pageSize,$curPage);
        return $this->callBack("评论列表",$this->SUCCESS_CODE,$data);
    }

    public function getCommentOption() {
        $where = array('type' => 'order_review');
        $res = array();
        $data = $this->configModel->getList($where);
        foreach ($data['list'] as $key => $item) {
            $valueOption = explode(chr(10),$item['value']);
            $res[$item['key']] = $valueOption;
        }
        return $this->callBack('评论选项列表',$this->SUCCESS_CODE,$res);
    }

    public function add() {
        $data = array();
        $user_id = I('user_id');
        $order_id = I('order_id');
        $type = I('type');
        $typeText = I('type_text');;
        if (!$user_id || !$order_id || !$type) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $count = $this->orderReviewModel->where(array('order_id' => $order_id,'user_id' => $user_id))->count();
        if ($count > 0) {
            $this->callBack('该订单您已评论过，请勿重复评论！',$this->ERROR_CODE);
        }
        $data['user_id'] = $user_id;
        $data['order_id'] = $order_id;
        $data['type'] = $type;
        $data['type_text'] = $typeText;
        $rs = $this->orderReviewModel->add($data);
        if ($rs !== false) {
            return $this->callBack('评论成功！',$this->SUCCESS_CODE);
        }
        return $this->callBack('评论失败！',$this->ERROR_CODE);
    }

}